﻿using Iworld.Author.Model;
using Iworld.Basis.Manager;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Author.Manager
{
    /// <summary>
    /// 赠送金币记录管理器
    /// </summary>
    [RegisterToManagerService]
    public class GiveGoldsRecordManager : ManagerBase<IModelToDbContextOfAuthor, GiveGoldsRecordManager.Actions, GiveGoldsRecord>
    {
        public GiveGoldsRecordManager(IModelToDbContextOfAuthor db)
            : base(db)
        {

        }


        /// <summary>
        /// 监听：在用户被移除之前，移除所有相关的记录
        /// </summary>
        /// <param name="info">数据集</param>
        [Listen(typeof(AuthorManager), AuthorManager.Actions.Remove, ExecutionOrder.Before)]
        public static void Monitor_RemoveRecordsWhenUserRemoving(InfoOfSendOnManagerService info)
        {
            IModelToDbContextOfAuthor db = (IModelToDbContextOfAuthor)info.Db;
            Model.Author model = (Model.Author)info.Model;

            db.GiveGoldsRecords.Where(x => x.From.Id == model.Id).ToList()
                .ForEach(x =>
                {
                    db.GiveGoldsRecords.Remove(x);
                });
            db.GiveGoldsRecords.Where(x => x.To.Id == model.Id).ToList()
                .ForEach(x =>
                {
                    x.To = null;
                });
        }


        /// <summary>
        /// 动作
        /// </summary>
        public enum Actions
        {
            /// <summary>
            /// 创建新的金币帐变记录
            /// </summary>
            Create,
            /// <summary>
            /// 修改金币帐变记录
            /// </summary>
            Update,
            /// <summary>
            /// 移除金币帐变记录
            /// </summary>
            Remove

        }

        /// <summary>
        /// 赠送金币数据集
        /// </summary>
        public interface IPackageForGiveGolds
        {
            /// <summary>
            /// 用户的存储指针
            /// </summary>
            int UserId { get; set; }

            /// <summary>
            /// 赠送的账号名称
            /// </summary>
            string Account { set; get; }

            /// <summary>
            /// 赠送的金币
            /// </summary>
            int Golds { set; get; }

            /// <summary>
            /// 描述
            /// </summary>
            string Description { set; get; }
        }
    }
}
